# Exit and cleanup file used by airoscript
# Copyright (C) 2010-2011 David Francos Cuartero
#        This program is free software; you can redistribute it and/or
#        modify it under the terms of the GNU General Public License
#        as published by the Free Software Foundation; either version 2
#        of the License, or (at your option) any later version.

#        This program is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#        GNU General Public License for more details.

#        You should have received a copy of the GNU General Public License
#        along with this program; if not, write to the Free Software

delete_vinterfaces(){ for i in $(awk "/$MON_PREFIX/ {print \$1}" <(airmon-ng)); do iw dev $i del; done; }
deconfigure(){ airmon-ng stop $wifi &> /dev/null ; delete_vinterfaces &>/dev/null; }
delete_dpath(){ grep "tmp" <<< $DUMP_PATH && rm -r $DUMP_PATH &>/dev/null;  } # First checks if there is tmp in dump_path, so dump_path will have to have tmp on it. That's because of some old trouble having dump_path as /home/xayon/ ... ¬¬ (Note: I've been asked more than twice so I clear it here: i didn't lost anything, I have all in sync with a git repository =) )

doexit(){
        check_function make_report || source $path/reporting 
        [[ -e $DUMP_PATH/$Host_MAC.key ]] && {
            yesno "Create a full report?" && {
                warn "${mark}Creating report"; make_report;
                yesno "Copy report to ~?" && { cp -f $DUMP_PATH/$Host_MAC.png $DUMP_PATH/$Host_MAC.report.html $DUMP_PATH/$Host_MAC.report.pdf $HOME/ &>/dev/null; }
            }
        }

        [[ $keep_exit_quiet == 0 ]] && {
            yesno "Stop monitor mode on $wifi and delete virtual interfaces? (Y/n) " && { warn "${mark}Cleaning interface"; deconfigure; }
            yesno 'Delete temporary data dir? (y/N) ' && { warn "${mark}Deleting $DUMP_PATH"; delete_dpath &>/dev/null; }
        } || { deconfigure; [[ $delete_files_on_exit == 1 ]] && delete_dpath &>/dev/null;  }
        [[ "$1" == "killme" ]] && pkill air $$ || exit # Needed because of autopwn
}
